package com.herench.bs.transfer;

import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.*;

/**
 * Created by lenovo on 2017/10/30.
 */
public class LoadExamListTransfer extends AbstractTransfer {
    public LoadExamListTransfer(String method, Client client,String messageMethod)   {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String cardNo = (String) params.get("cardNo");
        Calendar calendar = Calendar.getInstance();
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("DataNode1").addElement("DataRow");
        dataRowElement.addAttribute("START_DATE", startDate).addAttribute("END_DATE", endDate).addAttribute("ID_NO ", cardNo);
        return tplDoc.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (executeStatus.equals("0")) {
            List<Element> dataRows = document.getRootElement().element("ReturnData").element("Data").elements();
            List<Map<String, Object>> dataList = new ArrayList<>();
            if (dataRows != null && dataRows.size() > 0) {
                for (Element dataRow : dataRows) {
                    Map<String, Object> dataMap = new HashMap<>();
                    LoadExamListTransfer.LoadExamListInnerMessage dataRowMessage = new LoadExamListTransfer.LoadExamListInnerMessage(dataRow);
                    dataMap.put("deptCode", dataRowMessage.getDeptCode());
                    dataMap.put("description", dataRowMessage.getDescription());
                    dataMap.put("diagnosis", dataRowMessage.getDiagnosis());
                    dataMap.put("docName", dataRowMessage.getDocName());
                    dataMap.put("regTime", dataRowMessage.getRegTime());
                    dataMap.put("part", dataRowMessage.getPart());
                    dataMap.put("repTime", dataRowMessage.getRepTime());
                    dataMap.put("repId", dataRowMessage.getRepId());
                    dataMap.put("repName", dataRowMessage.getRepName());
                    dataMap.put("suggest", dataRowMessage.getSuggest());
                    dataList.add(dataMap);
                }
            }
            resMap.put("list", dataList);
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
            return resMap;
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);

        }
         return resMap;
    }

        private class LoadExamListInnerMessage implements InnerMessage {
            /**
             * 申请科室编码
             */
        private String deptCode;
            /**
             * 报告描述
             */
            private String description;
            /**
             * 报告诊断
             */
            private String diagnosis;

            /**
             * 申请医生姓名
             */
            private String docName;
            /**
             * 登记时间
             */
            private String regTime;
            /**
             * 检查部位
             */
            private String part;
            /**
             * 报告时间
             */
            private String repTime;
            /**
             * 检查报告号
             */
            private String repId;
            /**
             * 检查项目
             */
            private String repName;
            /**
             * 报告建议
             */
            private String suggest;

            public LoadExamListInnerMessage(Element element) {
                this.deptCode = element.attributeValue("REQ_DEPT");
                this.description = element.attributeValue("DESCRIPTION");
                this.diagnosis = element.attributeValue("CLIN_DIAG");
                this.docName = element.attributeValue("REQ_PHYSICIAN");
                this.regTime = element.attributeValue("REQ_DATE_TIME");
                this.part = element.attributeValue("EXAM_ITEM");
                this.repTime = element.attributeValue("REPORT_DATE_TIME");
                this.repId = element.attributeValue("REPORT_NO");
                this.repName = element.attributeValue("EXAM_CLASS");
                this.suggest = element.attributeValue("IMPRESSION");
            }
            public String getDeptCode() { return deptCode; }

            public String getDescription() { return description; }

            public String getDiagnosis() { return diagnosis; }

            public String getDocName() { return docName; }

            public String getRegTime() { return regTime; }

            public String getPart() { return part; }

            public String getRepTime() { return repTime; }

            public String getRepId() { return repId; }

            public String getRepName() { return repName; }

            public String getSuggest() { return suggest; }
        }
    }

